import { ref } from 'vue';

const useUser = () => {
  const user = ref({});

  const setUser = (info) => {
    if (!info) {
      localStorage.removeItem('DX_USER_INFO');
      user.value = {};
      return false;
    }
    user.value = info;
    localStorage.setItem('DX_USER_INFO', JSON.stringify(info));
    return user.value;
  };

  try {
    const userInfo = localStorage.getItem('DX_USER_INFO');
    user.value = JSON.parse(userInfo) || {};
  } catch (error) {
    user.value = {};
  }

  return [user, setUser];
};

export default useUser;
